#!/usr/bin/env node

const inquirer = require("inquirer");
const chalk = require("chalk");
const figlet = require("figlet");
const shell = require("shelljs");

const init = () => {
    // 显示脚本介绍
  console.log(
    chalk.green(
      figlet.textSync("J F CLI", {
        font: "Fuzzy",    // 字体
        horizontalLayout: "default",
        verticalLayout: "default"
      })
    )
  );
};

const askQuestions = () => {
    // 询问问题，创建对应名字的文件
  const questions = [
    {
      name: "FILENAME",
      type: "input",
      message: "file name?"
    },
    {
      type: "list",
      name: "EXTENSION",
      message: "file extension?",
      choices: [".rb", ".js", ".php", ".css"],
      filter: function(val) {
        return val.split(".")[1];
      }
    }
  ];
  return inquirer.prompt(questions);
};

const createFile = (filename, extension) => {
    // 创建文件
  const filePath = `${process.cwd()}/${filename}.${extension}`
  shell.touch(filePath);
  return filePath;
};

const success = filepath => {
  console.log(
    chalk.white.bgGreen.bold(`Done! ${filepath}`)
  );
};

const run = async () => {
  // 显示脚本介绍
  init();

  // 询问问题
  const answers = await askQuestions();
  const { FILENAME, EXTENSION } = answers;

  // 创建文件
  const filePath = createFile(FILENAME, EXTENSION);

  // 显示成功
  success(filePath);
};

run();